<--- %%NOBANNER%% --> indexw.sas
 BackForward

/*-------------------<---Start of Description-->---------------------\
| Return the first position of a word in the string;                 |
| Note: this function will also check to see if the word is separated|
|       by some special characters other then the regular characters;|
|---------------------<---End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<---Start of Files or Arguments Needed-->---------------|
| Inputs:                                                            |
|   var  - the input string you want to check against;               |
|   excerpt - the word you want to check;                            |
|------------------<---End of Arguments Needed-->--------------------|
|--------------------------------------------------------------------|
|------------------<---Start of Files Created-->---------------------|
| Usage: indexw(var,excerpt);                                        |
\-------------------<---End of Files Created-->---------------------*/
%macro indexw(var,excerpt);
%if (%sysfunc(index(%nrbquote(&var), %nrbquote(&excerpt)))=1) %then %do;
   %if (%eval(%length(%nrbquote(&var))+1) = %eval(%sysfunc(index(%nrbquote(&var), %nrbquote(&excerpt))) + %length(%nrbquote(&excerpt)))) 
      %then %sysfunc(index(%nrbquote(&var), %nrbquote(&excerpt)));
   %else %if (%eval(%length(%nrbquote(&var))+1) > %eval(%sysfunc(index(%nrbquote(&var), %nrbquote(&excerpt))) + %length(%nrbquote(&excerpt)))) %then %do;
      %if (%sysfunc(rxmatch(%sysfunc(rxparse($a|$d)), %nrbquote(%substr(%nrbquote(&var), %eval(%sysfunc(index(%nrbquote(&var), %nrbquote(&excerpt))) + %length(%nrbquote(&excerpt))), 1))))) %then 0;
      %else %sysfunc(index(%nrbquote(&var), %nrbquote(&excerpt)));
   %end;
   %else 0;
%end;
%else %if (%sysfunc(index(%nrbquote(&var), %nrbquote(&excerpt)))>1) %then %do;
   %if (%sysfunc(rxmatch(%sysfunc(rxparse($a|$d)), %nrbquote(%substr(%nrbquote(&var), %eval(%sysfunc(index(%nrbquote(&var), %nrbquote(&excerpt))) -1), 1))))) %then 0;
   %else %do; 
      %if (%eval(%length(%nrbquote(&var))+1) = %eval(%sysfunc(index(%nrbquote(&var), %nrbquote(&excerpt))) + %length(%nrbquote(&excerpt)))) %then 
         %sysfunc(index(%nrbquote(&var), %nrbquote(&excerpt)));
      %else %if (%eval(%length(%nrbquote(&var))+1) > %eval(%sysfunc(index(%nrbquote(&var), %nrbquote(&excerpt))) + %length(%nrbquote(&excerpt)))) %then %do;
          %if (%sysfunc(rxmatch(%sysfunc(rxparse($a|$d)), %nrbquote(%substr(%nrbquote(&var), %eval(%sysfunc(index(%nrbquote(&var), %nrbquote(&excerpt))) + %length(%nrbquote(&excerpt))), 1))))) %then 0;
          %else %sysfunc(index(%nrbquote(&var), %nrbquote(&excerpt)));
      %end;
      %else 0;
   %end;
%end;
%else 0;
%mend indexw;